import Vue from 'vue';
import { Message } from 'element-ui';

// 获取URL地址参数
const getUrlParam = name => {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  let r = window.location.search.substr(1).match(reg);
  if (r !== null) return decodeURIComponent(r[2]);
  return null;
};

// 获取code值对应文字
const errorCode = code => {
  let errorText = '';
  let language = localStorage.getItem('language');
  let codeValue = localStorage.getItem('errorCode');
  codeValue = codeValue ? JSON.parse(codeValue) : {};
  errorText = codeValue[language][code];
  return errorText;
};

// 根据接口返回的message值码弹出对应的操作信息
const toast = (code, duration = 1000) => {
  let message = '';
  let language = localStorage.getItem('language');
  let codeValue = localStorage.getItem('errorCode');
  codeValue = codeValue ? JSON.parse(codeValue) : {};
  message = codeValue[language][code];
  Message.success({
    message,
    duration
  });
};

// 消息提示-默认错误提示
const msgFn = (message, type = 'error', duration = 1500) => {
  Message({
    type,
    message,
    duration
  });
};

Vue.prototype.$toast = toast;
Vue.prototype.$msgFn = msgFn;

export { getUrlParam, errorCode };
